package source.view;

import java.awt.GridLayout;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;

import javax.swing.JLabel;
import javax.swing.JPanel;

import source.model.skill.Skill;
import source.model.type.GameObjectType;
import source.model.type.UnitType;
import source.view.viewobjects.ViewObjectWithStats;
import source.view.viewobjects.ViewUnit;

/**
 * Displays stats about the currently selected unit or structure.  Also
 * displays stats about the current player.
 * @author Steve
 *
 */
public class StatusViewport extends JPanel {
	private ViewData viewData;
	private Hashtable<String, String> stats;
	
	/* These are the player stats */
	private int playerId;
	private int playerOre;
	private int energyUsed;
	private int energyTotal;
	
	private JPanel playerStats;
	private JPanel selectedStats;
	
	/**
	 * Constructor
	 *
	 */
	public StatusViewport(ViewData viewData){
		this.viewData = viewData;
		stats = new Hashtable<String, String>();
		
		this.setLayout(new GridLayout(2,1));
		playerStats = new JPanel();
		selectedStats = new JPanel();
		
		this.add(playerStats);
		this.add(selectedStats);
		
		update();
	}
	
	/**
	 * Sets the visible player's id.
	 * @param id
	 */
	public void setPlayerId(int id){
		playerId = id;
	}
	
	/**
	 * Sets the current players Ore Stat.
	 * @param ore
	 */
	public void setPlayerOre(int ore){
		playerOre = ore;
	}
	
	/**
	 * Sets the current players used energy.
	 * @param energyUsed
	 */
	public void setEnergyUsed(int energyUsed){
		this.energyUsed = energyUsed;
	}
	
	/**
	 * Sets the current player's total energy.
	 * @param energyTotal
	 */
	public void setEnergyTotal(int energyTotal){
		this.energyTotal = energyTotal;
	}
	
	/**
	 * Add a stat to be displayed.
	 * @param stat
	 * @param value
	 */
	public void addStat(String stat, String value){
		stats.put(stat, value);
	}
	
	/**
	 * Change the stat to be displayed.
	 * Precondition: Stat has been added before.
	 * @param stat
	 * @param value
	 */
	public void modifyStat(String stat, String value){
		String result = stats.remove(stat);
		if(result != null){
			stats.put(stat, value);
		}
		else{
			MainScreen.writeToConsole("StatusViewport: Tried to modify a stat that doesn't exist");
		}
	}
	
	/**
	 * Update the status viewport with the most recently set statistics.
	 *
	 */
	public void update(){
		//updates the players visible stats.
		playerStats.removeAll();
		
		Hashtable<String, String> playerHashStats = viewData.getPlayerStats();
		JLabel label;
		String s = "<html><table>"
			+ "<tr><td>Player " + viewData.getCurrentPlayer() + " Stats</td><td></td></tr>"
			+ "<tr><td>ORE : </td><td>" + playerHashStats.get("Ore") + "</td</tr>"
			+ "<tr><td>Energy : </td><td>" + playerHashStats.get("Energy Used") + "/" + playerHashStats.get("Energy Total") + "</td></tr>"
			+ "</table></html>";
		label = new JLabel(s);
		playerStats.add(label);
		
		// Place whatever stats that we want need to display.
		selectedStats.removeAll();
		
		ViewObjectWithStats vob = viewData.getCurrentViewObject();
		
		if(vob != null){
			s = "<html><table>";
			switch(vob.objectID().getTypePrefix()){
			case 4: // Unit
				s += "<tr><td>Unit Type:</td><td> " + vob.getStats().get("Unit Type") + "</td></tr>";
				s += "<tr><td>Health:</td><td> " + vob.getStats().get("Current Health") + "/" + vob.getStats().get("Maximum Health") + "</td></tr>";
				
				ViewUnit vu = (ViewUnit) vob;
				Skill[] skills = vu.getUtype().skillSet();
				
				boolean first = true;
				for(Skill sk: skills){
					if(first){
						s += "<tr><td>Skills:</td><td>" + sk.skillName() + "</td></tr>";
						first = false;
					}else
						s += "<tr><td></td><td>" + sk.skillName() + "</td></tr>";
				}
				
				/*String skills = vob.getStats().get("Skill");
				if(skills == null){
					s += "<tr><td>Skill: </td><td>None</td></tr>";
				}
				else{
					StringTokenizer strTok = new StringTokenizer(skills, ",");
					while(strTok.hasMoreElements()){
						s += "<tr><td></td><td>" + strTok.nextToken() + "</td></tr>";
					}
				}*/
				
				s += "<tr><td>Movement Left: </td><td>" + vob.getStats().get("Moves Remaining");
				break;
				
			case 3: // Structure
				s += "<tr><td>Structure Type:</td><td> " + vob.getStats().get("Structure Type") + "</td></tr>";
				s += "<tr><td>Health:</td><td>" + vob.getStats().get("Current Health") + "/" + vob.getStats().get("Maximum Health");
				
				String queue = vob.getStats().get("Build Queue");
				if(!queue.equals("none")){
					StringTokenizer strTok = new StringTokenizer(queue, ",");
					while(strTok.hasMoreTokens()){
						s += "<tr><td></td><td>" + strTok.nextToken() + "</td></tr>";
					}
				}
				else{
					s += "<tr><td>Build Queue:</td><td>Empty</td></tr>";
				}
				
			}
			s += "</table></html>";
			label = new JLabel(s);
			selectedStats.add(label);
		}
		
		validate();
	}
}
